package daoService;

//import cn.smbms.dao.BaseDao;
import dao.UserDao;
import dao.UserDaoImpl;
import daoBean.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;
import org.junit.jupiter.api.Test;
import utils.JDBCUtils2;
/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */
public class UserServiceImpl implements UserService {
	private UserDao userDao;

	public UserServiceImpl() {
		userDao = new UserDaoImpl();
	}

		@Override
		public int addUser(User user) {
			// TODO Auto-generated method stub
		
/*
			try {
				connection = BaseDao.getConnection();
				connection.setAutoCommit(false);//开启JDBC事务管理
				int updateRows = userDao.add(connection,user);
				connection.commit();
				if(updateRows > 0){
					flag = true;
					System.out.println("add success!");
				}else{
					System.out.println("add failed!");
				}
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				try {
					System.out.println("rollback==================");
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}finally{
				//在service层进行connection连接的关闭
				BaseDao.closeResource(connection, null, null);
			}
			return flag;*/
			Connection connection = null;
			int res = 0;
			try {
				connection = JDBCUtils2.getConnection3();
				connection.setAutoCommit(false);//开启JDBC事务管理
				res = userDao.addUser(connection, user);
				connection.commit();
				if (res > 0){
					System.out.println("添加了");
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				try {
					System.out.println("出异常了rollback");
					connection.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			} finally {
				JDBCUtils2.closeResource(connection, null);
			}
			return res;
		}


	/*
		@Override
		public User login(String userCode, String userPassword) {
			// TODO Auto-generated method stub
			Connection connection = null;
			User user = null;
			try {
				connection = BaseDao.getConnection();
				user = userDao.getLoginUser(connection, userCode);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				BaseDao.closeResource(connection, null, null);
			}
		
			//匹配密码
			if(null != user){
				if(!user.getUserPassword().equals(userPassword))
					user = null;
			}
		
			return user;
		}
		@Override
		public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
			// TODO Auto-generated method stub
			Connection connection = null;
			List<User> userList = null;
			System.out.println("queryUserName ---- > " + queryUserName);
			System.out.println("queryUserRole ---- > " + queryUserRole);
			System.out.println("currentPageNo ---- > " + currentPageNo);
			System.out.println("pageSize ---- > " + pageSize);
			try {
				connection = BaseDao.getConnection();
				userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				BaseDao.closeResource(connection, null, null);
			}
			return userList;
		}
		@Override
		public User selectUserCodeExist(String userCode) {
			// TODO Auto-generated method stub
			Connection connection = null;
			User user = null;
			try {
				connection = BaseDao.getConnection();
				user = userDao.getLoginUser(connection, userCode);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				BaseDao.closeResource(connection, null, null);
			}
			return user;
		}
		@Override
		public boolean deleteUserById(Integer delId) {
			// TODO Auto-generated method stub
			Connection connection = null;
			boolean flag = false;
			try {
				connection = BaseDao.getConnection();
				if(userDao.deleteUserById(connection,delId) > 0)
					flag = true;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				BaseDao.closeResource(connection, null, null);
			}
			return flag;
		}*/

	public User getUserById(String id) {
		// TODO Auto-generated method stub
		User user = null;
		Connection conn = null;
		try {
//			connection = BaseDao.getConnection();
//			user = userDao.getUserById(connection,id);
			conn = JDBCUtils2.getConnection3();
//			user = userDao.getUserById(conn,id);
			user = userDao.getUserById(conn, id);
		} catch (Exception e) {
			// TODO: handle exception
			user = null;
			e.printStackTrace();

		} finally {
			JDBCUtils2.closeResource(conn, null);
		}
		return user;
	}

	@Test
	public int modify(User user) {
		// TODO Auto-generated method stub
		Connection connection = null;
		int res = 0;
/*		String id = "1";
		String phone ="13688889990";
		String userName = "系统管理员2";
		String userPassword = "12345678";
		String address = "ww的地址";
		User user = new User();
		user.setId(Integer.valueOf(id));
		user.setUserName(userName);
		user.setAddress(address);
		user.setUserPassword(userPassword);
		user.setPhone(phone);*/
		try {
			connection = JDBCUtils2.getConnection3();
			res = userDao.modify(connection, user);
			if (res > 0){
				System.out.println("大于0");
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtils2.closeResource(connection, null);
		}
		return res;
	}


	/*
	@Override
	public boolean updatePwd(int id, String pwd) {
		boolean flag = false;
		Connection connection = null;
		try{
			connection = BaseDao.getConnection();
			if(userDao.updatePwd(connection,id,pwd) > 0)
				flag = true;
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}
	@Override
	public int getUserCount(String queryUserName, int queryUserRole) {
		// TODO Auto-generated method stub
		Connection connection = null;
		int count = 0;
		System.out.println("queryUserName ---- > " + queryUserName);
		System.out.println("queryUserRole ---- > " + queryUserRole);
		try {
			connection = BaseDao.getConnection();
			count = userDao.getUserCount(connection, queryUserName,queryUserRole);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return count;
	}
	
}*/
}
